local tipsMgr = {}
local COLOR_CODE = {

}


local function getData(pack, index)
    local key = string.format("data%s", index)
    if not pack[key] then
        local dbgMsg = string.format(
            "[tipsMgr.makeTip][pack: %s] data%d is nil",
            pack.name, index)
        print(dbgMsg)
    end
    if type(pack[key]) ~= "table" then
        return pack[key]
    end
end


local function convertColor(color)
    return COLOR_CODE[color]
end


function tipsMgr.excute(func)
    return function(...)
        local startTime = os.clock()
        local res = func(...)
        local endTime = os.clock()
        local executionTime = endTime - startTime
        print("Execution time:", executionTime)
        return res
    end
end


-- 第1种，只有1级的
-- 第2种，多级的（1/2/3)
-- 第3种，数量型的
function tipsMgr.makeTip(pack)
    local tip = pack.tip or "？？？"
    if tip then

        local function convertFix(index)
            return getData(pack, index)
        end

        local convertPct = function(index)
            local data = getData(pack, index)
            if not data then
                return nil
            end
            local val = (tonumber(data) * 100)
            local res
            if (val % 1 < 0.01) then
                res = string.format("%.0f%%", val)
            else
                res = string.format("%.1f%%", val)
            end
            return res
        end

        tip = string.gsub(tip, '%<f(%d+)%>', convertFix)
        tip = string.gsub(tip, '%<p(%d+)%>', convertPct)
        tip = string.gsub(tip, '%<fs(%d+)%>', convertFixArr)
        tip = string.gsub(tip, '%<ps(%d+)%>', convertPctArr)
        tip = string.gsub(tip, '%<([%w_]+)%>', convertColor)
    end
    return tip
end



print(tipsMgr.excute(tipsMgr.makeTip)({
    name = "测试",
    tip = "攻击有<p1>的几率额外造成<f2>点伤害<p3>",
    data1 = 0.1,
    data2 = 180,
}))